﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Windows.Forms;
using BO;
using Model;
using Utils;

namespace ZiChengCode
{
    public partial class FrmUserLoad_mysql : Form
    {
        public FrmUserLoad_mysql()
        {
            InitializeComponent();
            Init();
        }

        private void btnTestContect_Click(object sender, EventArgs e)
        {
            //  SetConfig.GetDefaultTempate();
            MySqlEntity.UserName = txtUserName.Text;
            MySqlEntity.Password = txtUserPwd.Text;
            MySqlEntity.Port = int.Parse(txtServerPort.Text);
            MySqlEntity.Url = txtServerName.Text;
            if (string.IsNullOrEmpty(MySqlEntity.Url))
            {
                MessageBox.Show("服务器不能为空!", "连接提醒");
                txtServerName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(MySqlEntity.UserName))
            {
                MessageBox.Show("用户名不能为空!", "连接提醒");
                txtUserName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(MySqlEntity.Password))
            {
                MessageBox.Show("密码不能为空!", "连接提醒");
                txtUserPwd.Focus();
                return;
            }
            MySqlBo bo = new MySqlBo();
            if (bo.Contect())
            {
                MessageBox.Show(new NativeWindow(), "连接成功", "连接提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cbDatabase.Enabled = true;
                btnOK.Enabled = true;
                cbDatabase.DataSource = new MySqlBo().GetDbList();
                btnTestContect.Enabled = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string dbName = cbDatabase.SelectedItem.ToString();

            if (string.IsNullOrEmpty(dbName))
            {
                MessageBox.Show("请选择数据库");
                return;
            }
            MySqlEntity.DataBase = dbName;
            MySqlEntity.UserName = txtUserName.Text;
            MySqlEntity.Password = txtUserPwd.Text;
            MySqlEntity.Port = int.Parse(txtServerPort.Text);
            MySqlEntity.Url = txtServerName.Text;
            MySqlBo bo = new MySqlBo();
            if (bo.Contect())
            {
                this.Hide();
                new FrmMain().ShowDialog();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
        }

        private void Init()
        {
            txtServerName.Text = "127.0.0.1";
            txtServerPort.Text = "3306";
            InitButton();
        }

        private void txtServerName_TextChanged(object sender, EventArgs e)
        {
            InitButton();
        }

        private void InitButton()
        {
            btnTestContect.Enabled = true;
            btnOK.Enabled = false;
            cbDatabase.Enabled = false;
        }

        private void txtServerPort_TextChanged(object sender, EventArgs e)
        {
            InitButton();
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
            InitButton();
        }

        private void txtUserPwd_TextChanged(object sender, EventArgs e)
        {
            InitButton();
        }
    }
}
